package com.sn.test;

import com.sn.utils.PoolingHttpClient;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BaiduDemo {
    public static void main(String[] args) throws IOException {
        //使用工具类获取client连接池,获取client对象来请求网页
        PoolingHttpClient httpClient=new PoolingHttpClient();
        String content = httpClient.doGetHtml("https://news.baidu.com/");

        //直接使用jsoup解析content
        Document doc = Jsoup.parse(content, "utf8");
        //初步筛选blanks标签

        Elements a = doc.getElementsByAttributeValue("target","_blank");
        int size = a.size();
        int i=0;
        System.out.println(size);
        for (Element element : a) {
            String text = element.text();
            if(!StringUtils.isBlank(text)&&!text.contains("Android版下载")&&!text.contains("iPhone版下载")){
                //筛选掉无标题的非新闻
                System.out.println(text+"链接:  "+element.attr("href"));

                i++;
            }
        }
        System.out.println(i);

    }
}
